const NONE_TYPE = { key: 'none', value: '无' }
/** 方向 - 列表 */
const DIRECTION_OPTIONS = [
  { key: '3', value: '东' },
  { key: '5', value: '南' },
  { key: '7', value: '西' },
  { key: '1', value: '北' },
  { key: '2', value: '东北' },
  { key: '4', value: '东南' },
  { key: '6', value: '西南' },
  { key: '8', value: '西北' }
]

// 路口形状枚举
const ROAD_SHAPE = [
  { key: 'yx', value: 'I字形' },
  { key: 'sz', value: '十字形' },
  { key: 'tx', value: 'T形' },
  { key: 'hd', value: '环岛' },
  { key: 'ld', value: '路段' },
  { key: 'crk', value: '出入口' },
  { key: 'other', value: '其他:', showInput: true },
]

// 点位属性
const DOT_TYPE = [
  { key: '4', value: '无' },
  { key: '1', value: 'I类' },
  { key: '2', value: 'II类' },
  { key: '3', value: 'III类' },
]

// 信号机品牌
const SIGNAL_TYPE = [
  { key: 'unknown', value: '未知' },
  { key: 'xmz', value: '西门子' },
  { key: 'tk', value: '泰科' },
  { key: 'hx', value: '海信' },
  { key: 'yhl', value: '易华录' },
  { key: 'ls', value: '莱斯' },
  { key: 'ta', value: '同安' },
  { key: 'other', value: '其他:', showInput: true },
]

// 周围建筑物
const AROUND_BUILDING = [
  { key: 'syq', value: '商业区(购物中心、商圈等)' },
  { key: 'school', value: '学校及教育机构' },
  { key: 'hospital', value: '医院及医疗机构' },
  { key: 'office', value: '办公区(写字楼/产业园等)' },
  { key: 'jtsn', value: '交通枢纽(地铁站/公交站/火车站等)' },
  { key: 'dxggcs', value: '大型公共场所(体育场馆/会展中心等)' },
  { key: 'gywlyq', value: '工业/物流园区' },
  { key: 'zzq', value: '住宅区' },
  { key: 'other', value: '其他:', showInput: true },
]

// 车流量
const SIZE_TYPE = [
  { key: 'big', value: '大' },
  { key: 'middle', value: '中' },
  { key: 'lack', value: '少' },
]

const LANE_SHAPE = [
  { key: 'pz', value: '平直' },
  { key: 'wq', value: '弯曲' },
  { key: 'pd', value: '坡度' },
]

// 有、无、并且不需要填写内容
const HAS_OR_NONE = [
  { key: 'has', value: '有' },
  NONE_TYPE,
]

// 有、无、并且需要填写内容
const HAS_OR_NONE_NEED_INPUT = [
  { key: 'has', value: '有，编号：', showInput: true },
  NONE_TYPE,
]

// 有、无、混行
const HAS_OR_NONE_MIX = [
  { key: 'has', value: '有' },
  { key: 'mix', value: '混行' },
]

// 其他车道类型
const OTHER_LANE_TYPE = [
  NONE_TYPE,
  { key: 'gjyx', value: '公交优先车道' },
  { key: 'cxcd', value: '潮汐车道' },
  { key: 'kbdx', value: '可变导向车道' },
  { key: 'dzq', value: '待转区' },
]

// 信号灯杆类型
const SIGNAL_STEM_TYPE = [
  { key: 'LPole', value: 'L杆' },
  { key: 'uniPole', value: '单立柱' },
]

// 信号灯杆高度
const SIGNAL_STEM_HEIGHT = [
  { key: 'standard', value: '适合' },
  { key: 'needRaise', value: '需加高至7米' },
]

// 机动车信号灯形态
const VEHICLE_LAMP_TYPE = [
  { key: 'circleLamp', value: '圆形灯' },
  { key: 'arrowLamp', value: '箭头灯' },
  NONE_TYPE
]

// 机动车信号灯形态
const VEHICLE_LAMP_DIAM = [
  { key: '200', value: '200' },
  { key: '300', value: '300' },
  { key: '400', value: '400' },
]

// 电警杆横臂长度
// const POLICE_POLE_LENGTH = [
//   { key: 'fit', value: '适合' },
//   { key: 'needLengthening', value: '需加长' },
// ]

// 电警杆类型
const POLICE_POLE_TYPE = [
  { key: 'F', value: 'F杆' },
  { key: 'T', value: 'T杆' },
]

// 电警品牌
const POLICE_BRAND = [
  { key: 'has', value: '有,品牌：', showInput: true },
  NONE_TYPE
]

// 遮挡物类型
const SCREEN_TYPE = [
  NONE_TYPE,
  { key: 'tree', value: '树木' },
  { key: 'footbridge', value: '天桥/高架桥' },
  { key: 'billboard', value: '广告牌' },
  { key: 'other', value: '其他:', showInput: true },
]

// 输入框类型
const CONTENT_TYPE = {
  'INPUT': 'INPUT',
  'RADIO': 'RADIO',
  'CHECKBOX': 'CHECKBOX',
  'SLOT': 'SLOT',
}

// 方向枚举
const DIRECTION_IMAGE = [
  { key: 1, value: 'zuozhuan', },
  { key: 2, value: 'zhihang', },
  { key: 3, value: 'youzhuan', },
  { key: 4, value: 'diaotou', }
]

// 判断路段的KEY
// 条件一：如果点位形状选择了“路段”，则后面不显示“信号机品牌”、“信号灯杆”、“机动车信号灯”等栏的输入项
const LD_KEY = 'ld'

const NODE_KEY = NONE_TYPE.key

// 方向基本信息
const DIRECTION_CONFIG = [
  {
    key: 'motorway',
    value: '机动车道',
    child: [
      { key: 'motorwayFlow', value: '车流量(目测)', type: CONTENT_TYPE.RADIO, option: SIZE_TYPE },
      { key: 'motorwayShape', value: '车道形状', type: CONTENT_TYPE.RADIO, option: LANE_SHAPE },
      { key: 'motorwayNumber', value: '数量(个)', type: CONTENT_TYPE.SLOT },
    ]
  },
  {
    key: 'sidewalk',
    value: '人行道',
    child: [
      { key: 'sidewalkCross', value: '斑马线', type: CONTENT_TYPE.RADIO, option: HAS_OR_NONE },
      { key: 'sidewalkNumber', value: '人流量(目测)', type: CONTENT_TYPE.RADIO, option: SIZE_TYPE },
    ]
  },
  {
    key: 'bicycleLane',
    value: '非机动车道',
    child: [
      { key: 'bicycleLaneHas', value: '有无', type: CONTENT_TYPE.RADIO, option: HAS_OR_NONE_MIX },
      { key: 'bicycleLaneNumber', value: '车流量(目测)', type: CONTENT_TYPE.RADIO, option: SIZE_TYPE },
    ]
  },
  {
    key: 'otherLane',
    value: '其他车道',
    child: [
      { key: 'otherLaneType', value: '', type: CONTENT_TYPE.CHECKBOX, option: OTHER_LANE_TYPE },
    ]
  },
  {
    key: 'signalStem',
    value: '信号灯杆',
    hiddenKey: [LD_KEY],
    child: [
      { key: 'signalStemNoHas', value: '存在', type: CONTENT_TYPE.RADIO, option: HAS_OR_NONE_NEED_INPUT },
      { key: 'signalStemType', hiddenKey: NODE_KEY, relativeKey: 'signalStemNoHas', value: '类型', type: CONTENT_TYPE.RADIO, option: SIGNAL_STEM_TYPE },
      { key: 'signalStemHeight', hiddenKey: NODE_KEY, relativeKey: 'signalStemNoHas', value: '高度(目测)', type: CONTENT_TYPE.RADIO, option: SIGNAL_STEM_HEIGHT },
      { key: 'signalStemDistance', hiddenKey: NODE_KEY, relativeKey: 'signalStemNoHas', value: '离停止线距离(米，用测量仪器)', type: CONTENT_TYPE.INPUT },
    ]
  },
  {
    key: 'motorVehicleLamp',
    value: '机动车信号灯',
    hiddenKey: [LD_KEY],
    child: [
      { key: 'motorVehicleLampForm', value: '存在', type: CONTENT_TYPE.RADIO, option: VEHICLE_LAMP_TYPE },
      { key: 'motorVehicleLampNumber', hiddenKey: NODE_KEY, relativeKey: 'motorVehicleLampForm', value: '数量(组)', type: CONTENT_TYPE.INPUT },
      { key: 'motorVehicleLampDiam', hiddenKey: NODE_KEY, relativeKey: 'motorVehicleLampForm', value: '直径(毫米，目测)', type: CONTENT_TYPE.RADIO, option: VEHICLE_LAMP_DIAM },
    ]
  },
  {
    key: 'policePole',
    value: '电警杆/卡口杆',
    child: [
      { key: 'policePoleHasNo', value: '存在', type: CONTENT_TYPE.RADIO, option: HAS_OR_NONE_NEED_INPUT },
      { key: 'policePoleCarNumber', hiddenKey: NODE_KEY, relativeKey: 'policePoleHasNo', value: '横臂覆盖车道数', type: CONTENT_TYPE.INPUT },
      // { key: 'policePoleLength', hiddenKey: NODE_KEY, value: '横臂长度(目测)', type: CONTENT_TYPE.RADIO, option: POLICE_POLE_LENGTH },
      { key: 'policePoleType', hiddenKey: NODE_KEY, relativeKey: 'policePoleHasNo', value: '类型', type: CONTENT_TYPE.RADIO, option: POLICE_POLE_TYPE },
      { key: 'policePoleDistance', hiddenKey: NODE_KEY, relativeKey: 'policePoleHasNo', value: '离停止线距离(米，用测量仪器)', type: CONTENT_TYPE.INPUT },
    ]
  },
  {
    key: 'electricPolice',
    value: '电警',
    child: [
      { key: 'electricPoliceBrand', value: '存在', type: CONTENT_TYPE.RADIO, option: POLICE_BRAND },
      { key: 'electricPoliceMachineNumber', hiddenKey: NODE_KEY, relativeKey: 'electricPoliceBrand', value: '枪机数量(个)' },
      { key: 'electricPoliceLightNumber', hiddenKey: NODE_KEY, relativeKey: 'electricPoliceBrand', value: '补光灯数量(个)' },
    ]
  },
  {
    key: 'reverseCheckpoint',
    value: '反向卡口',
    child: [
      { key: 'reverseCheckpointBrand', value: '存在', type: CONTENT_TYPE.RADIO, option: POLICE_BRAND },
      { key: 'reverseCheckpointMachineNumber', hiddenKey: NODE_KEY, relativeKey: 'reverseCheckpointBrand', value: '枪机数量(个)' },
      { key: 'reverseCheckpointLightNumber', hiddenKey: NODE_KEY, relativeKey: 'reverseCheckpointBrand', value: '补光灯数量(个)' },
    ]
  },
  {
    key: 'ambientCondition',
    value: '遮挡物',
    child: [
      { key: 'ambientConditionScreen', value: '', type: CONTENT_TYPE.CHECKBOX, option: SCREEN_TYPE }
    ]
  },
  {
    key: 'otherDevice',
    value: '其他设备(标注清楚名称和数量)',
    child: [
      { key: 'otherDeviceText', value: '', type: CONTENT_TYPE.INPUT, inputType: 'textarea' }
    ]
  }
]

/**
 * 通过key值找到对应的value
 * @param {*} key 
 * @param {*} list 
 * @returns 
 */
const findValueByKey = (key, list) => {
  const result = list.find(item => item.key == key)
  return result?.value ?? key
}

// 时间格式化
const formatDate = (date, format = 'YYYY-MM-DD HH:mm') => {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');
  const seconds = String(date.getSeconds()).padStart(2, '0');

  return format
    .replace('YYYY', year)
    .replace('MM', month)
    .replace('DD', day)
    .replace('HH', hours)
    .replace('mm', minutes)
    .replace('ss', seconds);
}

// 存储信息到本地，防止信息丢失
const setLocal = (key, msg) => {
  window.localStorage.setItem(key, JSON.stringify(msg))
}

// 获取本地信息
const getLocal = (key) => {
  const data = window.localStorage.getItem(key)
  return data ? JSON.parse(data) : ''
}
// 清除缓存
const clearLocal = () => {
  window.localStorage.clear()
}

const baseHeader = { key: 'base', value: '基础信息' }
/**基础信息的默认值配置 */
const base = {
  time: formatDate(new Date()),
  user: '',
  roadName: '',
  roadPosition: '',
  roadShape: ROAD_SHAPE[0].key,
  roadShapeExt: '',
  dotType: DOT_TYPE[0].key,
  signalType: '',
  signalTypeExt: '',
  aroundBuilding: [],
  aroundBuildingExt: ''
}
/**车道的子项 */
const motorwayItem = {
  width: 3.5,
  direction: [2]
}
/**方向子项 */
const directionItem = {
  current: 0,
  motorwayNumber: 3,
  motorwayFlow: '',
  motorwayShape: LANE_SHAPE[0].key,
  motorwayList: [
    JSON.parse(JSON.stringify(motorwayItem)),
    JSON.parse(JSON.stringify(motorwayItem)),
    JSON.parse(JSON.stringify(motorwayItem))],
  signalStemNoHas: HAS_OR_NONE[0].key,
  signalStemType: SIGNAL_STEM_TYPE[0].key,
  signalStemHeight: SIGNAL_STEM_HEIGHT[0].key,
  motorVehicleLampDiam: VEHICLE_LAMP_DIAM[1].key,
  policePoleHasNo: HAS_OR_NONE[0].key,
  // policePoleLength: POLICE_POLE_LENGTH[0].key,
  electricPoliceBrand: POLICE_BRAND[0].key,
  reverseCheckpointBrand: POLICE_BRAND[0].key,
  reverseCheckpoint: POLICE_BRAND[0].key,
  ambientConditionScreen: [SCREEN_TYPE[0].key],
  otherLaneType: [OTHER_LANE_TYPE[0].key],
  motorVehicleLampForm: NODE_KEY,
  policePoleType: POLICE_POLE_TYPE[0].key
}

